package cn.tedu.crm.orders.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.tedu.crm.customer.mapper.CustomerFollowUpMapper;
import cn.tedu.crm.customer.mapper.CustomerMapper;
import cn.tedu.crm.customer.pojo.entity.Customer;
import cn.tedu.crm.customer.pojo.entity.CustomerFollowUp;
import cn.tedu.crm.deposit.pojo.vo.OrdersDepositVO;
import cn.tedu.crm.deposit.pojo.vo.ProjectVO;
import cn.tedu.crm.orders.mapper.DieMapper;
import cn.tedu.crm.orders.mapper.OrdersMapper;
import cn.tedu.crm.orders.pojo.dto.*;
import cn.tedu.crm.orders.pojo.entity.*;
import cn.tedu.crm.orders.pojo.vo.OrderDetailVO;
import cn.tedu.crm.orders.pojo.vo.OrdersImgVO;
import cn.tedu.crm.orders.pojo.vo.OrdersVO;
import cn.tedu.crm.orders.service.OrdersService;
import cn.tedu.crm.project.mapper.ProjectMapper;
import cn.tedu.crm.project.pojo.dto.ProjectAddParam;
import cn.tedu.crm.project.service.ProjectService;
import cn.tedu.crm.refund.mapper.RefundMapper;
import cn.tedu.crm.refund.pojo.entity.Refund;
import cn.tedu.crm.staff.mapper.StaffMapper;
import cn.tedu.crm.staff.pojo.dto.StaffQuery;
import cn.tedu.crm.staff.pojo.vo.StaffVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private RefundMapper refundMapper;

    @Autowired
    private CustomerFollowUpMapper customerFollowUpMapper;

    @Autowired
    private DieMapper dieMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ProjectService projectService;
    @Override
    public List<OrdersVO> load(OrdersQuery ordersQuery) {
        log.debug("加载订单信息业务,入参:{}",ordersQuery);
        List<OrdersVO> list = ordersMapper.load(ordersQuery);
        return list;
    }

    private Map<String,List<OrdersImgVO>> map = new HashMap<>();


    @Override
    public Map<String, List<OrdersImgVO>> loadImg(Long id) {
        log.debug("加载订单图片信息业务,入参:{}",id);
        Long orderId = id;
        List<OrdersImgVO> list = ordersMapper.loadImg(orderId);
        log.debug("加载订单图片信息业务,结果:{}",list);
        List<OrdersImgVO> ListGroup = new ArrayList<>();
//        for (OrdersImgVO ordersImgVO : list){
//            if (ordersImgVO.getType().equals("电位图")){
//                log.debug("图纸分组业务,入参:{}",ordersImgVO);
//                ListGroup.add(ordersImgVO);
//                log.debug("图纸分组业务,结果:{}",ListGroup);
//                list.remove(ordersImgVO);
//            }
//        }
        Iterator<OrdersImgVO> iterator = list.iterator();
        while (iterator.hasNext()) {
            OrdersImgVO item = iterator.next();
            if (item.getType().equals("电位图")){
                log.debug("图纸分组业务,入参:{}",item);
                ListGroup.add(item);
                log.debug("图纸分组业务,结果:{}",ListGroup);
                iterator.remove();
            }
        }

        log.debug("图纸分组业务,入参:{}",ListGroup);
        map.put("电位图",ListGroup);
        log.debug("图纸分组业务,入参:{}",list);
        map.put("水位图",list);
        log.debug("加载订单图片信息业务,结果:{}",map);
        return map;
    }

    @Override
    public void add(OrdersAddParam ordersAddParam) {
        log.debug("新增订单业务,入参:{}",ordersAddParam);
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersAddParam,orders);
        Long orderNum = (long)(Math.random()*1000000L);
        orders.setOrderNumber(orderNum.toString());
        orders.setOrderDate(new Date());
        orders.setCreateUser(StpUtil.getLoginIdAsLong());
        orders.setUpdateUser(StpUtil.getLoginIdAsLong());
        orders.setStaffId(ordersAddParam.getDesignerId());
        orders.setTransferProgress(10);
//        orders.setDesignerProgress(0);
        orders.setOrderCost(ordersAddParam.getOrderCost());
        orders.setCreateTime(new Date());
        orders.setUpdateTime(new Date());

        StaffQuery staffQuery = new StaffQuery();
        staffQuery.setId(ordersAddParam.getDesignerId());
        List<StaffVO> staffVOS = staffMapper.selectStaff(staffQuery);
        if (!staffVOS.isEmpty()) {
            orders.setDesigner(staffVOS.get(0).getUserName());
        }
        Customer customer = new Customer();
        customer.setId(ordersAddParam.getUserId());
        customer.setOrderNumber(orderNum.toString());
        customer.setUpdateTime(new Date());
        CustomerFollowUp customerFollowUp = new CustomerFollowUp();
        customerFollowUp.setCustomerId(ordersAddParam.getUserId());
        customerFollowUp.setStatus("40");
        customerFollowUp.setUpdateTime(new Date());
        customerFollowUpMapper.updateById(customerFollowUp);
        /*
        * 插入客户订单编号
        * */
        customerMapper.updateById(customer);
        ordersMapper.insert(orders);
    }

    @Override
    public void imgAdd(String url, Long id, String type) {
        log.debug("新增订单图纸业务参数,地址:{},订单Id:{},图纸类型:{}",url,id,type);
        Drawing drawing = new Drawing();
        drawing.setOrderId(id);
        drawing.setImgUrl(url);
        drawing.setType(type);
        drawing.setCreateTime(new Date());
        drawing.setUpdateTime(new Date());
        drawing.setSubmitter(StpUtil.getLoginIdAsLong());
        drawing.setUpdateUser(StpUtil.getLoginIdAsLong());
        int i = ordersMapper.imgAdd(drawing);
        log.debug("新增订单图纸业务结果,插入的图片数:{}",i);
        Orders orders = new Orders();
        orders.setId(id);
        orders.setTransferProgress(20);
        orders.setUpdateTime(new Date());
        orders.setUpdateUser(StpUtil.getLoginIdAsLong());
        ordersMapper.update(orders);
        ProjectAddParam projectAddParam = new ProjectAddParam();
        projectAddParam.setOrderId(id);
        projectService.addProject(projectAddParam);
    }

    @Override
    public Long loadByStaffId(Long staffId, Date createTime, Date assess) {
        log.debug("根据员工id查询订单业务,入参:{}",staffId);
        OrdersQueryByStaffId ordersQueryByStaffId = new OrdersQueryByStaffId();
        ordersQueryByStaffId.setStaffId(staffId);
        ordersQueryByStaffId.setCreateTime(createTime);
        ordersQueryByStaffId.setAssess(assess);
        Long l = ordersMapper.loadByStaffId(ordersQueryByStaffId);
        return l;
    }

    @Override
    public void delete(Long id) {
        log.debug("删除订单业务,入参:{}",id);
        ordersMapper.delete(id);
    }

    @Override
    public void addScheme(OrdersSchemeParam ordersSchemeParam) {
        log.debug("新增订单预演记录业务,入参:{}",ordersSchemeParam);
        Scheme scheme = new Scheme();
        scheme.setCompere(ordersSchemeParam.getCompere());
        scheme.setParticipate(ordersSchemeParam.getParticipate());
        scheme.setLogs(ordersSchemeParam.getLogs());
        scheme.setCreateTime(new Date());
        scheme.setUpdateTime(new Date());
        ordersMapper.addScheme(scheme);
        log.debug("新增订单预演记录业务,结果:{}",scheme);
        Orders orders = new Orders();
        orders.setId(ordersSchemeParam.getId());
        orders.setSchemeId(scheme.getId());
        orders.setUpdateTime(new Date());
        orders.setUpdateUser(StpUtil.getLoginIdAsLong());
        orders.setTransferProgress(80);
        ordersMapper.update(orders);
    }

    private static Long reId = null;
    @Override
    public void refundAdd(String url, Long id) {
        log.debug("新增订单退款图片上传业务,入参:{},:{}",url,id);
        RefundApplication refundApplication = new RefundApplication();
        refundApplication.setOrderId(id);
        refundApplication.setContract(url);
        refundApplication.setCreateTime(new Date());
        refundApplication.setUpdateTime(new Date());
        refundApplication.setCreateUser(StpUtil.getLoginIdAsLong());
        refundApplication.setUpdateUser(StpUtil.getLoginIdAsLong());
        ordersMapper.refundAdd(refundApplication);
        reId= refundApplication.getId();
    }

    @Override
    public void refundInsert(OrdersRefundParam ordersRefundParam) {
        log.debug("新增订单退款原因业务,入参:{}",ordersRefundParam);
        RefundApplication refundApplication = new RefundApplication();
        refundApplication.setId(reId);
        refundApplication.setReasonOrder(ordersRefundParam.getReasonOrder());
        refundApplication.setReasonMoney(ordersRefundParam.getReasonMoney());
        ordersMapper.refundAddReason(refundApplication);
        Orders orders = new Orders();
        orders.setId(ordersRefundParam.getOrderId());
        orders.setTransferProgress(60);
//        orders.setDesignerProgress(20);
        orders.setUpdateTime(new Date());
        orders.setUpdateUser(StpUtil.getLoginIdAsLong());
        ordersMapper.update(orders);
        log.debug("新增订单退款原因业务,结果:{},:{}",refundApplication,orders);
        Refund refund = new Refund();
        refund.setOrderId(ordersRefundParam.getOrderId());
        refund.setRefundStatus("1");
        refundMapper.insertOrderId(refund);
        log.debug("插入退单表订单id,结果:{}",refund);
    }

    @Override
    public OrderDetailVO details(Long id) {
        log.debug("订单详情业务,入参:{}",id);
        OrderDetailVO orderDetailVO = ordersMapper.details(id);
        log.debug("订单详情业务,结果:{}",orderDetailVO);
        return orderDetailVO;
    }

    @Override
    public Long getUserId(Long id) {
        return ordersMapper.getUserId(id);
    }

    @Override
    public void imgDel(Map<String, List<OrdersImgVO>> mapImg) {
        List<OrdersImgVO> listW = mapImg.get("水位图");
        List<OrdersImgVO> listE = mapImg.get("电位图");
        for (int i = 0; i < listW.size(); i++){
            ordersMapper.imgDel(listW.get(i).getId());
        }
        for (int i = 0; i < listE.size(); i++){
            ordersMapper.imgDel(listE.get(i).getId());
        }
    }

    @Override
    public ProjectVO selectPeojectById(Long id) {
        log.debug("工程订单表根据客户id查询订单表数据层;{}",id);
        return ordersMapper.selectPeojectById(id);
    }

    @Override
    public void dieOrder(DieOrderParam param) {
        log.debug("订单终止业务,入参:{}", param);
        Die die = new Die();
        die.setOrderId(param.getOrderId());
        die.setCreateTime(new Date());
        die.setUpdateTime(new Date());
        die.setCause(param.getCause());
        int insert = dieMapper.insert(die);
        if (insert > 0){
            Orders orders = new Orders();
            orders.setId(param.getOrderId());
            orders.setTransferProgress(70);
            orders.setUpdateTime(new Date());
            orders.setUpdateUser(StpUtil.getLoginIdAsLong());
            orders.setDesignerProgress(30);
            ordersMapper.update(orders);
        }
    }


    /*定金表根据客户id查询订单表*/
    @Override
    public OrdersDepositVO selectOrdersById(Long id) {
        log.debug("定金表根据客户id查询订单表数据层;{}",id);
        return ordersMapper.selectOrdersByIdCustomer(id);
    }
}
